package servlet;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.swetake.util.Qrcode;

import common.Servlet;
import jp.sourceforge.qrcode.QRCodeDecoder;
import jp.sourceforge.qrcode.data.QRCodeImage;

public class Qrcoding extends Servlet {

	private static final long serialVersionUID = 1L;

	public void process(HttpServletRequest request, HttpServletResponse response) throws Exception {

		response.setContentType("image/jpeg;charset=UTF-8");// 设定输出的类型
		OutputStream output = response.getOutputStream();
		BufferedImage bufferedImage = encoderQRCode(request.getParameter("site"));
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		ImageIO.write(bufferedImage, "jpg", output);
		ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
		baos.close();
		bais.close();
	}

	public BufferedImage encoderQRCode(String content) {
		try {
			int version = 5;
			Qrcode qrcodeHandler = new Qrcode();
			qrcodeHandler.setQrcodeErrorCorrect('M');
			qrcodeHandler.setQrcodeEncodeMode('B');
			qrcodeHandler.setQrcodeVersion(version);
			byte[] contentBytes = content.getBytes("UTF-8");
			BufferedImage bufferedImage = new BufferedImage(67 + 12 * (version - 1), 67 + 12 * (version - 1), BufferedImage.TYPE_INT_RGB);
			Graphics2D gs = bufferedImage.createGraphics();
			gs.setBackground(Color.WHITE);
			gs.clearRect(0, 0, 67 + 12 * (version - 1), 67 + 12 * (version - 1));
			gs.setColor(Color.BLACK);
			int pixoff = 2;

			if (contentBytes.length > 0 && contentBytes.length < 600) {
				boolean[][] codeOut = qrcodeHandler.calQrcode(contentBytes);
				for (int i = 0; i < codeOut.length; i++) {
					for (int j = 0; j < codeOut.length; j++) {
						if (codeOut[j][i]) {
							gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);
						}
					}
				}
			} else {
				System.err.println("QRCode content bytes length = " + contentBytes.length + " not in [ 0,600 ]. ");
			}
				int width_4 = (67 + 12 * (version - 1)) / 4;  
	            int width_8 = width_4 / 2;  
	            int height_4 = (67 + 12 * (version - 1))/ 4;  
	            int height_8 = height_4 / 2;  
	            Image img = ImageIO.read(this.getServletContext().getResourceAsStream("/images/et.jpg"));  
	            gs.drawImage(img, width_4 + width_8, height_4 + height_8,width_4,height_4, null);  
	            gs.dispose();    
	            bufferedImage.flush();  
            
			gs.dispose();
			bufferedImage.flush();
			return bufferedImage;

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	public String decoderQRCode(final BufferedImage bufferedImage) {
		String decodedData = null;
		try {
			QRCodeDecoder decoder = new QRCodeDecoder();
			decodedData = new String(decoder.decode(new QRCodeImage() {
				public int getHeight() {
					return bufferedImage.getHeight();
				}
				public int getPixel(int x, int y) {
					return bufferedImage.getRGB(x, y);
				}

				public int getWidth() {
					return bufferedImage.getWidth();
				}
			}), "UTF-8");

		} catch (Exception e) {
			e.printStackTrace();
		}
		return decodedData;
	}

}
